<!--
 * @Description: 车辆过户
 * @Autor: 刘诗文
 * @Date: 2020-11-10 09:44:25
 * @LastEditors: 马毅
 * @LastEditTime: 2021-05-19 15:45:33
-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="105" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      selectionType="single"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      columnsRef="customerRepair_customerinfo_ownerVehicleManagement_main"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="
            () => {
              this.handleTransfer('add', {});
            }
          "
          >新建过户</el-button
        >
        <AjaxButton size="small" type="primary" icon="el-icon-download" :click="ownerExportData">导出</AjaxButton>
      </template>
    </FilterTable>
    <Drawer :visible.sync="actions.visible" :title="actions.title" width="60%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addVehicleChange :type.sync="actions.type" :formData="this.resData" @close="closeHandler" />
    </Drawer>
    <BaseDialog
      :visible.sync="checkActions.visible"
      :title="checkActions.title"
      width="90%"
      :closable="true"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <transferCheckDetail :type.sync="checkActions.type" :formData="resData" @close="checkCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { isFormEmpty, notifyAction, camelCaseObject, findTableItem, findFormItem } from '@/utils';
import addVehicleChange from './addVehicleChange';
import transferCheckDetail from './transferCheckDetail';
import { exportExcelFile } from '@/utils/index';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
import { getOwnerInfo, getOwnerExcelList, getProvince, getCitys, getDistrict, getRegion } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { getTransferList, getVehicleTransferExport } from '@/api/customerRepair/vehicleOwnerManagement/vehicleChange';

export default {
  name: 'ownerVehicleManageIndex',
  components: {
    addVehicleChange,
    transferCheckDetail
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.resData = {};
    // isHidden: 'none';
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getTransferList,
      fetchParams: { pageNum: 1, xhrAbort: true },
      columns: this.createTableColumns(),
      actions: { visible: false, type: '', title: '' },
      checkActions: { visible: false, type: '', title: '' }
    };
  },
  created() {
    // this.getAllUser();
    // this.getProvine();
  },
  mounted() {
    this.BaseTable = this.$refs.table;
    // 获取全部人员信息
    this.getAllUser();
    // this.menuUserAction();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        {
          type: 'INPUT',
          label: '原车主姓名',
          fieldName: 'ownerName'
        },
        {
          type: 'INPUT',
          label: '新车主姓名',
          fieldName: 'newOwnerName'
        },
        {
          type: 'RANGE_DATE',
          label: '过户日期',
          style: { minWidth: '200px' },
          fieldName: 'transferDateBegin|transferDateEnd'
        },
        {
          type: 'INPUT',
          label: '新车主手机',
          fieldName: 'newMobile'
        },
        {
          type: 'SELECT',
          label: '审核状态',
          fieldName: 'checkStatus',
          itemList: this.createDictList(1261)
        }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table.$refs.pageTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div style="text-align:center">
                <el-button
                  size="mini"
                  type="text"
                  disabled={props.row.checkStatus === 12611003 || (props.row.isIfUpload == 0 && props.row.checkStatus === 12611001) ? false : true}
                  onClick={() => this.handleTransfer('edit', props.row)}
                >
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.detail(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '审核状态',
          dataIndex: 'checkStatus',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.checkStatus, 1261)}</span>;
          }
        },
        {
          title: '是否上传成功',
          dataIndex: 'isIfUpload',
          showOverflowTooltip: true,
          dictItems: [
            { text: '否', value: 0 },
            { text: '是', value: 1 }
          ]
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '车牌号',
          dataIndex: 'license',
          width: 90,
          showOverflowTooltip: true
        },
        {
          title: '申请日期',
          dataIndex: 'creatAt',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '审核日期',
          dataIndex: 'checkAt',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '新车主编号',
          dataIndex: 'ownerNoNew',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '新车主姓名',
          dataIndex: 'newOwnerName',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: '新车身份证号',
          dataIndex: 'newCertificateNo',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: '新车主手机',
          dataIndex: 'newMobile',
          width: 100,
          showOverflowTooltip: true
        },
        {
          title: '原车主编号',
          dataIndex: 'ownerNoOld',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '原车主姓名',
          dataIndex: 'ownerName',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '原车主身份证号',
          dataIndex: 'oldCertificateNo',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: '原车主手机',
          dataIndex: 'mobile',
          width: 90,
          showOverflowTooltip: true
        },
        {
          title: '操作人',
          dataIndex: 'operator',
          dictItems: [],
          showOverflowTooltip: true
        },
        {
          title: '过户时间',
          dataIndex: 'transferDate',
          showOverflowTooltip: true
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          showOverflowTooltip: true
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          showOverflowTooltip: true
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          showOverflowTooltip: true
        },
        {
          title: '审核信息',
          dataIndex: 'checkInfo',
          showOverflowTooltip: true
        }
      ];
    },
    // 新建/编辑过户
    handleTransfer(type, row) {
      if (type === 'add') {
        this.actions = { visible: true, title: '车辆过户申请', type: 'add' };
      } else {
        this.actions = { visible: true, title: '车辆过户申请编辑', type: 'edit' };
        this.resData = row;
      }
    },
    // 审核信息明细
    detail(row) {
      this.checkActions = { visible: true, title: '审核信息', type: 'detail' };
      this.resData = row;
    },
    closeHandler(refesh) {
      this.actions.visible = false;
      refesh && (this.fetchParams = { ...this.fetchParams });
    },
    checkCloseHandler(refesh) {
      this.checkActions.visible = false;
      refesh && (this.fetchParams = { ...this.fetchParams });
    },
    // 所有用户
    async getAllUser() {
      const res = await getAllUser();
      if (res.resultCode === 200) {
        if (res.errMsg) {
          return notifyAction(res.errMsg, 'error');
        } else {
          this.columns.find(item => item.dataIndex === 'operator').dictItems = res.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
        }
      }
    },
    // 下载
    async ownerExportData() {
      await getVehicleTransferExport(this.fetchParams).then(res => {
        console.log(res);
        if (res.status !== 'failure') {
          exportExcelFile(res.data, '车辆过户信息');
        } else {
          notifyAction('导出失败', 'error');
        }
      });
    }
  }
};
</script>
<style lang="less" scoped></style>
